package com.wms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.WarehouseAreaDto;
import com.wms.dto.WarehouseAreaPageDto;
import com.wms.mapper.WarehouseAreaMapper;
import com.wms.pojo.PageResult;
import com.wms.pojo.WarehouseArea;
import com.wms.service.WarehouseAreaService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class WarehouseAreaServiceImpl extends ServiceImpl<WarehouseAreaMapper, WarehouseArea> implements WarehouseAreaService {
    @Override
    public PageResult queryPage(WarehouseAreaPageDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<WarehouseArea> list = getBaseMapper().queryPage(dto);
        Page<WarehouseArea> areas = (Page<WarehouseArea>) list;
        return new PageResult(areas.getTotal(),areas.getResult());
    }

    @Override
    public void saveWarehouseArea(WarehouseAreaDto dto) {
        WarehouseArea warehouseArea = WarehouseArea.builder().warehouseId(dto.getWarehouseId())
                .createBy(dto.getCreateBy())
                .name(dto.getName())
                .category(dto.getCategory())
                .status(dto.getStatus())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        getBaseMapper().insert(warehouseArea);
    }

    @Override
    public void updateWarehouseAreaById(WarehouseAreaDto dto) {
        WarehouseArea warehouseArea = WarehouseArea.builder().id(dto.getId())
                .updateTime(LocalDateTime.now())
                .warehouseId(dto.getWarehouseId())
                .name(dto.getName())
                .category(dto.getCategory())
                .createBy(dto.getCreateBy())
                .status(dto.getStatus())
                .build();
        getBaseMapper().updateById(warehouseArea);
    }
}
